"""
Case Type   : gs_checkse工具功能
Case Name   : 验证gs_checkse的A3检测项确保账户登录失败尝试次数配置检测与修复成功
Create At   : 2024/10/25
Owner       : lihongji
Description :
    1.设置failed_login_attempts参数为0后检测A3项
    2.修复A3不符合安全规范项目后再次检测
    3.数据库查看failed_login_attempts的值
    4.设置failed_login_attempts参数为1000后检测A3项
    5.对正确的A3项进行修复后查看failed_login_attempts的值
    6.设置failed_login_attempts参数为10后检测A3项、修复B3
    7.查看数据库中failed_login_attempts的值
Expect      :
    1.设置成功，打印failed_login_attempts告警信息
    2.不打印告警信息
    3.failed_login_attempts值为10
    4.设置成功，不打印failed_login_attempts告警信息
    5.修复成功，failed_login_attempts值为1000
    6.设置成功，不打印告警信息
    7.failed_login_attempts的值为10
History     :
"""
import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro


class GS_CHECKSE(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.primaryNode = Node('PrimaryDbUser')
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.env_path = macro.DB_ENV_PATH

    def test_gscheckse(self):

        step = ('----step1:设置failed_login_attempts参数为0后检测A3项 '
                'expect:设置成功，打印failed_login_attempts告警信息----')
        self.log.info(step)
        excute_msg = self.sh_primary.execute_gsguc(
            'reload ', self.constant.GSGUC_SUCCESS_MSG,
            "failed_login_attempts=0")
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A3 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn("Ensure correct configuration of account login "
                      "failure attempt counts.", check_res, '执行失败' + step)
        self.assertTrue(excute_msg)

        step = '----step2:修复A3不符合安全规范项目后再次检测  expect:不打印告警信息----'
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B3 --detail;'
                          f'gs_checkse -i A3 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertNotIn("Ensure correct configuration of account login "
                         "failure attempt counts.", check_res, '执行失败' + step)

        step = ('----step3:数据库查看failed_login_attempts的值 '
                'expect:failed_login_attempts值为10----')
        self.log.info(step)
        check_sql = "show failed_login_attempts"
        create_res = self.sh_primary.execut_db_sql(check_sql)
        self.log.info(create_res)
        self.assertIn('10', create_res, '执行失败' + step)

        step = ('----step4:设置failed_login_attempts参数为1000后检测A3项 '
                'expect:设置成功，不打印failed_login_attempts告警信息----')
        self.log.info(step)
        excute_msg = self.sh_primary.execute_gsguc(
            'reload ', self.constant.GSGUC_SUCCESS_MSG,
            "failed_login_attempts=1000")
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A3 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertNotIn("Ensure correct configuration of account login "
                         "failure attempt counts.", check_res,
                         '执行失败' + step)
        self.assertTrue(excute_msg)

        step = ('----step5:对正确的A3项进行修复后查看failed_login_attempts的值'
                ' expect:修复成功，failed_login_attempts值为1000----')
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B3 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        check_sql = "show failed_login_attempts"
        create_res = self.sh_primary.execut_db_sql(check_sql)
        self.log.info(create_res)
        self.assertNotIn("Ensure correct configuration of account login "
                         "failure attempt counts.", check_res, '执行失败' + step)
        self.assertIn("1000", create_res, '执行失败' + step)

        step = ('----step6:设置failed_login_attempts参数为10后检测A3项、修复B3'
                'expect:设置成功，不打印告警信息----')
        self.log.info(step)
        excute_msg = self.sh_primary.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            "failed_login_attempts = 10")
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A3 --detail;'
                          f'gs_checkse -i B3 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertTrue(excute_msg)
        self.assertNotIn("Ensure correct configuration of account login "
                         "failure attempt counts.", check_res, '执行失败' + step)

        step = ('----step7:查看数据库中failed_login_attempts的值 '
                'expect:failed_login_attempts的值为10----')
        self.log.info(step)
        check_sql = "show failed_login_attempts;"
        create_res = self.sh_primary.execut_db_sql(check_sql)
        self.log.info(create_res)
        self.assertIn('10', create_res, '执行失败' + step)

    def tearDown(self):
        step = '----step8:清理环境 expect:成功----'
        self.log.info(step)
        excute_msg = self.sh_primary.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            "failed_login_attempts='10'")
        restart_msg = self.sh_primary.restart_db_cluster()
        self.assertTrue(excute_msg)
        self.assertTrue(restart_msg)
        self.log.info(f'----{os.path.basename(__file__)} end----')